package com.tanhua.server.controller;

import com.tanhua.domain.db.UserFreeze;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.FreezeApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.serivce.CommentService;
import com.tanhua.server.serivce.MovementsMQService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("comments")
@Slf4j
public class CommentsController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private MovementsMQService movementsMQService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private FreezeApi freezeApi;


    /**
     * 接口名称：评论列表
     * 接口路径：GET/comments
     */
    @GetMapping
    public ResponseEntity<Object> queryCommentsList(String movementId,
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "10")Integer pagesize){
        return commentService.queryCommentsList(movementId,page,pagesize);
    }

    /**
     * 接口名称：评论-提交
     * 接口路径：POST/comments
     */
    @PostMapping
    public ResponseEntity<Object> saveComments(@RequestBody Map<String,String> paramMap) {

        //这个方法没有获取userid 调用方法获取下
        Long userId = UserHolder.getUserId();

        //=======================================================
        //冻结评论判断
        UserInfo userInfo = userInfoApi.findById(userId);
        String userStatus = userInfo.getUserStatus();
        if (userStatus.equals("2")) {
            String freezingTime = redisTemplate.opsForValue().get("user_freezing_time" + userId);
            if (freezingTime==null) {
                //封禁到期后，登陆的时候修改用户的状态
                userInfo.setUserStatus("1");
                userInfo.setReasonsForThawing(" ");
                userInfoApi.update(userInfo);
                //删除冻结记录
                freezeApi.delectUnfreeze((int) userId.longValue());

            }else {
                //未到期判断用户冻结类型
                UserFreeze userFreeze = freezeApi.select(userId);
                if (userFreeze.getFreezingRange().equals(2)) {
                    Map<String,String> result = new HashMap<>();
                    result.put("message","账号发言冻结，请联系客服");
                    return ResponseEntity.ok(result);
                }

            }
        }

        String movementId = paramMap.get("movementId");
        String comment = paramMap.get("comment");

        // 发送mq消息，用于推荐计算系统计算操作分值，最终录入分值信息到mongodb中。
        movementsMQService.likePublishMsg(movementId);



        return commentService.saveComments(movementId,comment);
    }

}
